//
//  MSTabBarView.m
//  MSCustomTabBarController
//
//  Created by MrSong on 16/6/7.
//  Copyright © 2016年 MrSong. All rights reserved.
//

#import "MSTabBarView.h"

@interface MSTabBarView ()

@property (nonatomic, strong) NSMutableArray *items;

@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) NSInteger preIndex;
@property (nonatomic, weak) IBOutlet UIImageView *bgImageView;
@property (nonatomic, strong) NSArray *listAry;
@end

#define BTNTAG 100
#define IMAGETAG 1000
#define TITLETAG  10000
@implementation MSTabBarView

#pragma mark - Life Cycle

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    if (IS_IPHONE_X) {
        
         self.bgImageView.frame =  CGRectMake(0, -25, BOUNDWIDTH, self.frame.size.height+25);
        
    } else {
         self.bgImageView.frame =  CGRectMake(0, -15, BOUNDWIDTH, self.frame.size.height+15);
        
    }
   
    
    self.currentIndex = 0;

    
    [self setCurrentTab];
    self.items = [NSMutableArray arrayWithCapacity:5];
    self.listAry =  @[@{@"title":@"首页",@"commonImage":@"tab_home",@"selectImage":@"tab_home_select"},
                      @{@"title":@"选车",@"commonImage":@"tab_choose",@"selectImage":@"tab_choose_select"},
                      @{@"title":@"车友圈",@"commonImage":@"tab_carcircle",@"selectImage":@"tab_carcircle_select"},
                      @{@"title":@"服务",@"commonImage":@"tab_news",@"selectImage":@"tab_news_select"},
                      @{@"title":@"我的",@"commonImage":@"tab_me",@"selectImage":@"tab_me_select"}];

    [self createUI];
}

-   (void)createUI
{
    float width = BOUNDWIDTH/self.listAry.count;
    
    [self.listAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dict = obj;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(width*idx, 0, width , self.frame.size.height+15);
        btn.tag = idx;
        [btn addTarget:self action:@selector(selectItemIndex:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgImageView addSubview:btn];
        [self.items addObject:btn];
        
        
        UIImageView *logoImageView = [[UIImageView alloc] init];
        logoImageView.image = IMG([dict objectForKey:@"commonImage"]);
        logoImageView.tag= idx+IMAGETAG;
        [btn addSubview:logoImageView];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = [dict objectForKey:@"title"];
        titleLabel.textColor = Common_TextLowColor;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = FONT_SIZE(12);
        titleLabel.tag= idx+TITLETAG;
        [btn addSubview:titleLabel];
        
        float imgWidth =  20;
        if (idx  == 0) {
            titleLabel.textColor = [UIColor blackColor];
            logoImageView.image = IMG([dict objectForKey:@"selectImage"]);

        }
        
        if (idx == 2) {
              imgWidth = 30;
        }
                           
                           
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(12);
            make.bottom.mas_equalTo(btn.mas_bottom).offset(-5);
            make.left.mas_equalTo(btn.mas_left).offset(0);
        }];
        
        [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(imgWidth);
            make.height.mas_equalTo(imgWidth);
            make.bottom.mas_equalTo(titleLabel.mas_top).offset(-5);
            make.left.mas_equalTo(btn.mas_left).offset((width-imgWidth)/2);
        }];
        
                        
        
        
        
        
    }];
    
        
    
    
}


-  (void)setSwitchTabItem:(NSInteger)index
{
    
    self.currentIndex = index;
    [self setCurrentTab];
    // 让代理来处理切换viewController的操作
    if ([self.viewDelegate respondsToSelector:@selector(msTabBarView:didSelectItemAtIndex:)]) {
        [self.viewDelegate msTabBarView:self didSelectItemAtIndex:index];
    }
    
}

#pragma mark - Actions

- (void)selectItemIndex:(UIButton *)sender
{
    // button的tag对应tabBarController的selectedIndex
    // 设置选中button的样式
 
    UIButton *btn = sender;
   
    self.currentIndex = btn.tag;
    NSLog(@"selectIndex%ld",self.currentIndex);
    NSLog(@"tag%ld",sender.tag);

 
    // 让代理来处理切换viewController的操作
    if ([self.viewDelegate respondsToSelector:@selector(msTabBarView:didSelectItemAtIndex:)]) {
        [self.viewDelegate msTabBarView:self didSelectItemAtIndex:btn.tag];
        [self setCurrentTab];
    }
}

- (IBAction)clickCenterItem:(id)sender
{
    // 让代理来处理点击中间button的操作
    if ([self.viewDelegate respondsToSelector:@selector(msTabBarViewDidClickCenterItem:)]) {
        [self.viewDelegate msTabBarViewDidClickCenterItem:self];
    }
}

#pragma mark - Setter

- (void)setCurrentTab
{
    // 先把上次选择的item设置为可用
    UIButton *lastItem = _items[self.preIndex];
    NSDictionary *lastDict  = self.listAry [self.preIndex];
    UIImageView *lastImageView = [lastItem viewWithTag:self.preIndex+IMAGETAG];
    UILabel *lastTitleLabel = [lastItem viewWithTag:self.preIndex+TITLETAG];
    lastTitleLabel.textColor = Common_TextLowColor;
    lastImageView.image = IMG([lastDict objectForKey:@"commonImage"]);
    lastItem.enabled = YES;
    // 再把这次选择的item设置为不可用
    UIButton *item = _items[self.currentIndex];
    item.enabled = NO;
    NSDictionary *itemDict  = self.listAry [self.currentIndex];
    UIImageView *itemImageView = [item viewWithTag: self.currentIndex+IMAGETAG];
    UILabel *itemTitleLabel = [item viewWithTag: self.currentIndex+TITLETAG];
    itemTitleLabel.textColor = [UIColor blackColor];
    itemImageView.image = IMG([itemDict objectForKey:@"selectImage"]);
    self.preIndex = self.currentIndex;
}

@end
